﻿namespace Socut
{
    using System;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    using System.Reflection;

    public class Reader
    {
        private int _ConnIndex;
        private CAcc myAcc;
        private CSql mySql;
        private OleDbDataReader OleDR;
        private SqlDataReader SqlDR;

        public Reader(OleDbDataReader dr)
        {
            this.myAcc = new CAcc();
            this.mySql = new CSql();
            if (CLink.Type(this._ConnIndex) == 0)
            {
                this.OleDR = dr;
            }
        }

        public Reader(SqlDataReader dr)
        {
            this.myAcc = new CAcc();
            this.mySql = new CSql();
            if (CLink.Type(this._ConnIndex) == 1)
            {
                this.SqlDR = dr;
            }
        }

        public Reader(string SQL)
        {
            this.myAcc = new CAcc();
            this.mySql = new CSql();
            this.ExecuteReader(SQL, null, 0);
        }

        public Reader(string SQL, string[,] para)
        {
            this.myAcc = new CAcc();
            this.mySql = new CSql();
            this.ExecuteReader(SQL, para, 0);
        }

        public Reader(string SQL, int ConnIndex)
        {
            this.myAcc = new CAcc();
            this.mySql = new CSql();
            this.ExecuteReader(SQL, null, ConnIndex);
        }

        public Reader(string SQL, string[,] para, int ConnIndex)
        {
            this.myAcc = new CAcc();
            this.mySql = new CSql();
            this.ExecuteReader(SQL, para, ConnIndex);
        }

        public void Close()
        {
            if (CLink.Type(this._ConnIndex) == 1)
            {
                this.SqlDR.Close();
                this.mySql.Close();
            }
            else
            {
                this.OleDR.Close();
                this.myAcc.Close();
            }
        }

        private void ExecuteReader(string SQL, string[,] para, int ConnIndex)
        {
            if (CLink.Type(ConnIndex) == 1)
            {
                this.mySql.Open(ConnIndex);
                SqlCommand cmd = new SqlCommand(SQL, this.mySql.ConnSql);
                cmd.CommandTimeout = 0;
                CParameter.SqlserverParameters(SQL, para, cmd, this.mySql.ConnSql);
                this.SqlDR = cmd.ExecuteReader();
            }
            else
            {
                this.myAcc.Open(ConnIndex);
                OleDbCommand command2 = new OleDbCommand(SQL, this.myAcc.ConnAcc);
                command2.CommandTimeout = 0;
                CParameter.AccessParameters(SQL, para, command2, this.myAcc.ConnAcc);
                this.OleDR = command2.ExecuteReader();
            }
            this._ConnIndex = ConnIndex;
        }

        public bool Read()
        {
            if (CLink.Type(this._ConnIndex) == 1)
            {
                return this.SqlDR.Read();
            }
            return this.OleDR.Read();
        }

        public object this[string cs]
        {
            get
            {
                if (CLink.Type(this._ConnIndex) == 1)
                {
                    return this.SqlDR[cs];
                }
                return this.OleDR[cs];
            }
        }
    }
}

